//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 12
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace GetItemAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            // TODO: Supply a valid attachment Id to retrieve
            //
            // GetItemAttachment(binding, yourAttachmentId);
        }

        /// <summary>
        /// Return the embedded item within an item attachment (Listing 12-23)
        /// </summary>
        /// <param name="binding">binding to use for EWS calls</param>
        /// <param name="attachmentId">Id of the item attachment</param>
        /// <returns>Embedded ItemType from attachment</returns>
        /// 
        public static ItemType GetItemAttachment(
                                  ExchangeServiceBinding binding,
                                  RequestAttachmentIdType attachmentId)
        {
            GetAttachmentType getAttachmentRequest = new GetAttachmentType();

            // Just for fun, let's request the mime content and text body on our 
            // attachment.
            AttachmentResponseShapeType responseShape = new AttachmentResponseShapeType();
            responseShape.BodyType = BodyTypeResponseType.Text;
            responseShape.BodyTypeSpecified = true;
            responseShape.IncludeMimeContent = true;
            responseShape.IncludeMimeContentSpecified = true;

            // Also, let's grab an extended property just to show that we can do that 
            // with our response shape.
            //
            PathToExtendedFieldType useTNEFPath = new PathToExtendedFieldType();
            useTNEFPath.PropertySetId = "{00062008-0000-0000-C000-000000000046}";
            useTNEFPath.PropertyId = 0x8582;
            useTNEFPath.PropertyIdSpecified = true;
            useTNEFPath.PropertyType = MapiPropertyTypeType.Boolean;

            responseShape.AdditionalProperties = new BasePathToElementType[] { 
                                        useTNEFPath };
            getAttachmentRequest.AttachmentShape = responseShape;
            getAttachmentRequest.AttachmentIds = new RequestAttachmentIdType[] { 
                                        attachmentId };

            // Now make the actual call.
            //
            GetAttachmentResponseType response =
                            binding.GetAttachment(getAttachmentRequest);
            AttachmentInfoResponseMessageType responseMessage =
                 response.ResponseMessages.Items[0] as
                         AttachmentInfoResponseMessageType;

            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                ItemAttachmentType itemAttachment = responseMessage.Attachments[0] as
                        ItemAttachmentType;
                // Make sure we are dealing with an item attachment
                //
                if (itemAttachment == null)
                {
                    throw new ArgumentException("Attachment Id must refer to an item " +
                              "attachment", "attachmentId");
                }
                ItemType item = itemAttachment.Item;

                // Now show the subject and the userTNEF prop
                //
                Console.WriteLine("Subject: " + item.Subject == null ? "<NULL>" :
                                  item.Subject);

                if (item.ExtendedProperty.Length > 0)
                {
                    Console.WriteLine("UseTNEF? " + (string)item.ExtendedProperty[0].Item);
                }
                return item;
            }
            else
            {
                throw new Exception("Failed to get item attachment.  Response code: " +
                    responseMessage.ResponseCode.ToString());
            }
        }
    }
}
